package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.FileType;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.Page;
import com.kaishengit.util.db.RowMapper;

public class fileTypeDao {
	
	private DBHelp<FileType> db = new DBHelp<FileType>();
	
	
	public List<FileType> findAll(){
		
		String sql="select id,name,createtime,userid,projectid from t_filetype";
		
		return db.executeQueryToList(sql, new FtRowMapper());
		
		
	}
	
	
	public class FtRowMapper implements RowMapper<FileType>{

		public FileType mapRow(ResultSet rs) throws SQLException {
			
			FileType ft = new FileType();
			
			ft.setId(rs.getString("id"));
			ft.setName(rs.getString("name"));
			ft.setCreatetime(rs.getString("createtime"));
			ft.setUserId(rs.getString("userid"));
			ft.setProjectId(rs.getString("projectid"));
			return ft;
		}
		
		
	}



	private int count(String projectId) {
		String sql="select count(*) from t_filetype where projectid =?";
		return db.executeQuery(sql,projectId).intValue();
	}



	public Page<FileType> findByPage(String projectId, int pageNum) {
		Page<FileType> page = new Page<FileType>(count(projectId),pageNum);
		
		String sql = "select id,name,userid,projectid,createtime from t_filetype where projectid =? limit ?,?";
		List<FileType> list = db.executeQueryToList(sql, new FtRowMapper(),projectId,page.getStart(),page.getPageSize());
		page.setList(list);
		return page;
	}

}
